/**
 * Created by Administrator on 2016/8/22.
 */
var comm={
    /**
     * 发送按钮添加三态
     * @param btn
     */
    renderBtn:function(btn){
        btn.onmouseover=function(){
            btn.style.background="#317ef3";
        };
        btn.onmousedown=function(){
            btn.style.background="#2964bb";
        };
        btn.onmouseup=function(){
            btn.style.background="#317ef3";
        };
        btn.onmouseout=function(){
            btn.style.background="";
        }
    },
    /**
     * ajax获取
     * @param type
     * @param url
     * @param handler
     */
    ajax : function(type,url,handler){
        var xhr;
        if(XMLHttpRequest){
            xhr = new XMLHttpRequest();

        }else{
            xhr = new ActiveXObject("Microsoft.XMLHTTP");
        }
        xhr.open(type,url,true);
        xhr.send();
        xhr.onreadystatechange = function(){
            if(xhr.readyState == 4){
                if(xhr.status == 200){
                    if(handler){
                        handler();
                    }
                }else{
                    alert("发送失败，请重新发送！");
                }
            }
        }
    },

    /**
     * 阻止冒泡
     */
    stopBubble : function (ev){
        //IE下
        if(!ev){
            event.cancelBubble = true;
        }
        else{
            ev.stopPropagation();
        }
    },

    /**
     * 阻止浏览器默认行为(兼容各个浏览器)
     */

    preventDefault: function (evt){
        if(!evt){
            event.returnValue  = false;
        }
        else{
            evt.preventDefault();
        }
    },

    /**
     *  事件添加监听器兼容浏览器
     *  监听事件和普通事件的区别？
     *  1，同一个元素上，对同一个事件类型，可以多次绑定不会被覆盖
     *  2，可以对事件进行捕获
     */
    eventListener : function(node,type,handler,isBubble){
        //非IE
        if(typeof attachEvent == "undefined"){
            node.addEventListener(type,handler,isBubble);
        }
        else{
            node.attachEvent("on"+type,handler,isBubble);
        }
    }


};

/**
 * 获取元素id、class、Tagname
 * @param str
 * @returns {*}
 */
function $(str) {
    if(str.indexOf("#") != -1) {
        var id = str.substring(1);
        return document.getElementById(id);
    }
    else if(str.indexOf(".") != -1) {
        var className = str.substring(1);
        //非IE
        if(document.getElementsByClassName){
            return document.getElementsByClassName(className);
        }
        //IE
        else{
            //获取所有元素标签
            var arr = document.getElementsByTagName("*");
            var result = [];
            for(var i = 0;i < arr.length;i++){
                //获取所有元素标签的class属性值
                var classNames = arr[i].className;

                var arrList = classNames.split(" ");

                for(var j = 0;j < arrList.length;j++){
                    if(arrList[j] == className){
                        result.push(arr[j]);//获取class的元素
                        break;
                    }
                }
            }
            return result;
        }
    }
    else {
        return document.getElementsByTagName(str);
    }

}
var cookie = {
    /**
     * 设置cookie
     * @param key cookie名
     * @param value cookie值
     * @param expires 有效时间 number型 如：输入7 表示有效期为7天
     * @param path cookie路径
     * @param domain cookie域名
     * @param issecure cookie是否安全设置
     */
    setCookie : function(key,value,expires,path,domain,issecure){
        var str = encodeURIComponent(key) + "="+ encodeURIComponent(value);
        if(typeof expires == "number"){
            var date = new Date();
            date.setDate(date.getDate() + expires);
            str += ";expires=" + date;
        }
        if(path){
            str += ";path=" + path;
        }
        if(domain){
            str += ";domain=" + domain;
        }
        if(issecure){
            str += ";secure";
        }
        document.cookie = str;
    },
    /**
     * 移除cookie
     * @param key
     */
    removeCookie : function(key){
        /*this(当前对象)*/cookie.setCookie(key,"",-1);
    },
    /**
     * 通过cookie的name值，获取cookie的value
     * @param key
     * @returns {*}
     */
    getCookie : function(key){
        var str = document.cookie;

        var arr = str.split("; ");

        for(var j = 0;j<arr.length;j++){
            if(arr[j].indexOf(key) != -1){
                var temp = arr[j].split("=");
                if(temp[0]==key){
                    return decodeURIComponent(temp[1]);
                }
            }
        }

        return null;
    },
    getCookieFromMulti : function (key1,key2) {

        var value = cookie.getCookie(key1);

        if(!value){
            return;
        }

        if (!key2) {
            return value[0];
        }
        var  arr2 = value.split("&");
        for (var j = 0; j < arr2.length; j++) {
            if (arr2[j].indexOf(key2) != -1) {
                var temp1 = arr2[j].split("=");
                if (temp1[0] == key2) {
                    return decodeURIComponent(temp1[1]);
                }
            }
        }
        return null;
    },

    setCookieAtMulti : function(key1,key2,value,expires){
        var val = this.getCookie(key1);
        var subValList = val.split("&");
        for(var i=0;i<subValList.length;i++){
            if(subValList[i].indexOf(key2) != -1){
                var temp = subValList[i].split("=");
                if(temp[0] == key2){
                    subValList[i] = key2 + "=" + value;
                    val = subValList.join("&");
                    this.setCookie(key1,val,expires||7);
                    break;
                }
            }
        }

    }

};
//冒泡排序
function fn(arr){
    //var arr=[1,3,0,5];
    for(i=0;i<=arr.length-1;i++)
        {
            for(j=0;j<=arr.length-i-1;j++)
            {
            if(arr[j]>arr[j+1])
                {
                  var temp=arr[j];
                  arr[j]=arr[j+1];
                  arr[j+1]=temp;    
                }
            }

        }
}
//求数组中最大最小值
function max(arr){
    //var arr=[1,3,0,5];
    for(var i=0;i<arr.length;i++)
    {
        if(arr[0]<arr[i])
        {
            arr[0]=arr[i];
        }   
    }
}
//统计重复字母出现次数
function repeat(str){
    //var str = "abbfdhjsskbkj";
    for(var i=97;i<=122;i++)
    {
        var num = 0;
        for(var j=0;j<str.length;j++)
        {
            if(str.charAt(j)==String.fromCharCode(i))
            {
                num++;
                console.log(j);
            }                   
        }           
            if(num!=0)
            {
                document.write(String.fromCharCode(i) + "出现的次数" + num +"<br>");
            }
    }   
}
//去除字符串中的空格
function blank(str){
    //里面有空格也可打印
        //var str = "  ab d h   ";
        var first = 0;
        for(var i=0;i<str.length;i++){
            var code = str.charAt(i);
            if(code!=" "){
                first = i;
                break;
            }
        }
        var last = 0;
        for(i=str.length-1;i>=0;i--){
            var code = str.charAt(i);
            if(code!=" "){
            last=i;
            break;
            }
        }
        console.log("左边空格有" + first);
        console.log("右边空格有" + (str.length-last-1));
        console.log(str.substring(first,last+1));
}

function unique1(str) {
    //字符串去重
    var arr;
    if(typeof str=="string"){
        arr = str.split("");
    }else{
        arr = str;
    }
    var result = [];
    // [1,2,4,3,4,4,3,6];
    // result [1,2,4,3]
    for (var i = 0; i < arr.length; i++) {
        // 是否为重复数字的标识
        var isRepeat = false;
        for (var j = 0; j < result.length; j++) {
            if (arr[i] == result[j]) {
                isRepeat = true;
                break;
            }
        }
        if (!isRepeat) {
            result.push(arr[i]);
        }
    }
    if(typeof str=="string"){
        return result.join("");
    }
    return result;
}

//随机验证码
function RandomCheck(node){
    var arr=[];
    //函数封装
    function sum(a,b){
        for(var i=a;i<=b;i++){
            arr.push(String.fromCharCode(i));
        }
    }
    sum(97,122);
    sum(65,90);
    sum(48,57);
    function refresh(){
        var num1 = Math.floor(Math.random()*arr.length);
        var num2 = Math.floor(Math.random()*arr.length);
        var num3 = Math.floor(Math.random()*arr.length);
        var num4 = Math.floor(Math.random()*arr.length);
        node.innerHTML = arr[num1] + arr[num2] + arr[num3] + arr[num4];
    }
}
//倒计时
function gettime(node){
    var startTime = new Date('2016/08/25 00:00:00');
    var nowTime = new Date();
    var time = startTime-nowTime;       
    var day = Math.floor(time/1000/60/60/24);
    var hours = Math.floor(time/1000/60/60%24);
    var minute = Math.floor(time/1000/60%60);
    var second = Math.floor(time/1000%60);
        node.innerHTML="还剩" +day +"天"+ hours +"小时"+ minute+"分钟" + second+"秒";
}
//setInterval(gettime,1000);